<?php
	class Note extends Table {
		public $noNote;
		
		public $note;
		public $bareme;
		public $coefficient;
		public $dateAjout;
		public $description;
		
		public $discipline;
		public $etudiant;
		
		public function __construct($noEtudiant, $noDiscipline, $note, $bareme = 20, $coefficient = 1, $dateAjout = '', $description = '') {
			parent::__construct();
			
			$this->noNote = -1;
			
			$this->etudiant = new Etudiant('', '');
			$this->etudiant->noEtudiant = $noEtudiant;
			
			$this->discipline = new Discipline('');
			$this->discipline->identifiant = $noDiscipline;
			
			$this->dateAjout = (empty($dateAjout) ? date('Y-m-d') : $dateAjout);
			$this->bareme = $bareme;
			$this->note = $note;
			$this->coefficient = $coefficient;
		}
		
		public function ajouter() {
			$st = $this->db->prepare("INSERT INTO Note VALUES(NULL, ?, ?, ?, ?, ?, ?, ?");
			$st->execute(array($this->note,
								$this->bareme,
								$this->coefficient,
								$this->dateAjout,
								$this->description,
								$this->etudiant->noEtudiant,
								$this->discipline->identifiant));
								
			$this->noNote = $this->db->lastInsertId();
		}
		
		public function enregistrer() {	
			if ($this->noNote == -1) {
				$this->ajouter();
			}
			else {
				$this->modifier();
			}
		}
		
		public function modifier() {
			$st = $this->db->prepare("UPDATE Note SET note=?,bareme=?,coefficient=?,dateAjout=?,descriptionNote=?,noDiscipline=?,noEtudiant  WHERE noNote=?");
			
			$st->execute(array($this->note,
								$this->bareme,
								$this->coefficient,
								$this->dateAjout,
								$this->description,
								$this->etudiant->noEtudiant,
								$this->discipline->identifiant,
								$this->noNote));
		}
		
		public function completer() {
			if (is_numeric($this->noNote) && $this->noNote > 0) {
				$sql = "SELECT * FROM Note WHERE noNote=?";
				
				$st = $this->db->prepare($sql);
				$st->execute(array($this->noNote));
				
				$donnees = $st->fetch(PDO::FETCH_ASSOC);
			
				$this->note = $donnees['note'];
				$this->bareme = $donnees['bareme'];
				$this->coefficient = $donnees['coefficient'];
				
				$this->etudiant = new Etudiant('', '');
				$this->etudiant->noEtudiant = $donnees['noEtudiant'];
				
				$this->discipline = new Discipline('');
				$this->discipline->identifiant = $donnees['noDiscipline'];
			}
			else {
				// throw new AbsenceInvalideException();
			}
		}
		
		static public function recupererAvecID($id) {
			$note = new Note('', '', '');
			$note->noNote = $id;
			$note->completer();
			
			return $note;
		}
		
		static public function recupererAvecNoEtudiant($no) {
			$notes = array();
			
			$sql = "SELECT * FROM Note WHERE noEtudiant=?";
			$st = DB::get_instance()->prepare($sql);
			$st->execute(array($no));
			
			foreach ($st->fetchAll(PDO::FETCH_ASSOC) as $element) {
				$notes[] = Note::recupererAvecID($element['noNote']);
			}
			
			return $notes;
		}
	}
?>